﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace BootyBay.StoreAppMgmtCore
{
    public class UserProfileRepository
    {
        public static string GetCurrentUserDisplayName()
        {
            StringBuilder username = new StringBuilder(1024);
            uint size = (uint)username.Capacity;
            GetUserNameEx(EXTENDED_NAME_FORMAT.NameDisplay, username, ref size);

            string output= username.ToString();

            if(string.IsNullOrEmpty(output))
                output = "Unknown User";

            return output;
        }

        //reference: http://social.msdn.microsoft.com/Forums/en-US/02baba2b-4c1a-4126-aa0a-8e2e7fd07173/get-users-display-name-not-just-username?forum=csharpgeneral
        private enum EXTENDED_NAME_FORMAT
        {
            NameUnknown = 0,
            NameFullyQualifiedDN = 1,
            NameSamCompatible = 2,
            NameDisplay = 3,
            NameUniqueId = 6,
            NameCanonical = 7,
            NameUserPrincipal = 8,
            NameCanonicalEx = 9,
            NameServicePrincipal = 10,
            NameDnsDomain = 12
        }

        [DllImport("secur32.dll", CharSet = CharSet.Auto)]
        private static extern int GetUserNameEx(EXTENDED_NAME_FORMAT nameFormat, StringBuilder userName,
           ref uint userNameSize);

    }
}
